package com.bignerdranch.android.criminalintent;

import java.util.Date;
import java.util.UUID;

import org.json.JSONException;
import org.json.JSONObject;

public class Crime {
	private static final String JSON_ID = "id";
	private static final String JSON_TITLE = "title";
	private static final String JSON_SOLVED = "solved";
	private static final String JSON_DATE = "date";
	private static final String JSON_PHOTO = "photo";
	private static final String JSON_SUSPECT = "suspect";
	private static final String JSON_PHONE = "phone";
	
	private UUID mId;
	private String mTitle;
	private Date mDate;
	private boolean mSolved;
	private Photo mPhoto;
	private String mSuspect;
	private String mPhone;
	
	public Crime(){
		mId = UUID.randomUUID();
		mDate = new Date();
		mTitle = "";
	}
	
	public Crime(JSONObject jo) throws JSONException{
		mId = UUID.fromString(jo.getString(JSON_ID));
		mTitle = jo.getString(JSON_TITLE);
		mSolved = jo.getBoolean(JSON_SOLVED);
		mDate = new Date(jo.getLong(JSON_DATE));
		if(jo.has(JSON_PHOTO)){
			mPhoto = new Photo(jo.getJSONObject(JSON_PHOTO));
		}
		if(jo.has(JSON_SUSPECT)){
			mSuspect = jo.getString(JSON_SUSPECT);
		}
		if(jo.has(JSON_PHONE)){
			mPhone = jo.getString(JSON_PHONE);
		}
	}
	
	public String getTitle() {
		return mTitle;
	}

	public void setTitle(String mTitle) {
		this.mTitle = mTitle;
	}

	public UUID getId() {
		return mId;
	}

	public Date getDate() {
		return mDate;
	}

	public void setDate(Date mDate) {
		this.mDate = mDate;
	}

	public boolean isSolved() {
		return mSolved;
	}

	public void setSolved(boolean mSolved) {
		this.mSolved = mSolved;
	}
	
	public Photo getPhoto() {
		return mPhoto;
	}

	public void setPhoto(Photo mPhoto) {
		this.mPhoto = mPhoto;
	}
	
	public String getSuspect() {
		return mSuspect;
	}

	public void setSuspect(String mSuspect) {
		this.mSuspect = mSuspect;
	}
	
	public String getPhone() {
		return mPhone;
	}

	public void setPhone(String mPhone) {
		this.mPhone = mPhone;
	}

	public JSONObject toJSON() throws JSONException{
		JSONObject jo = new JSONObject();
		jo.put(JSON_ID, mId.toString());
		jo.put(JSON_TITLE, mTitle);
		jo.put(JSON_SOLVED, mSolved);
		jo.put(JSON_DATE, mDate.getTime());
		if(mPhoto != null){
			jo.put(JSON_PHOTO, mPhoto.toJson());
		}
		jo.put(JSON_SUSPECT, mSuspect);
		jo.put(JSON_PHONE, mPhone);
		return jo;
	}
	
	@Override
	public String toString() {
		return getTitle();
	}
}
